<?php
namespace app\admin\controller;

class Setup extends Base
{
    public function user()
    {
    	if (input("get.user_name")) {
            $where["user_name"] = ["like","%".input("get.user_name")."%"];
            $this->assign("user_name",input("get.user_name"));
        }else{
            $this->assign("user_name",'');
        }
        $where["id"] = ["gt",0]; 
        $count = db('ad_users')->where($where)->count();
        $page = new \think\Page($count,10);
        $list = db('ad_users')->where($where)->limit($page->firstRow.','.$page->listRows)->select();
        $ad_group = db('ad_group')->select();

        $group = [];
        foreach ($ad_group as $key => $value) {
        	$group[$value["id"]] = $value["name"];
        }
        $this->assign('group', $group);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function useradd()
    {
    	if (request()->isPost()) {
    	
    		$add["user_name"] = input("post.user_name");
    		$add["group_id"] = input("post.group_id");
    		$add["pass_word"] = md5(config("afs.pass_prefix").md5(input("post.pass_word")));
    		$add["login_time"] = 0;
            $add["login_ip"] = '';
            $add["last_login_time"] = 0;
            $add["last_login_ip"] = '';
            if (empty($add["user_name"]) || empty($add["group_id"]) || empty($add["pass_word"])) {
                $this->error('所有内容不得为空！');
            }else{
                $back = db('ad_users')->insert($add);
                if ($back !== false) {
                    $this->success('添加成功', '/admin/setup/user.html');
                }else{
                    $this->error('添加失败');
                }
            }

        }

        $ad_group = db('ad_group')->select();
        $group = [];
        foreach ($ad_group as $key => $value) {
        	$group[$value["id"]] = $value["name"];
        }
        $this->assign('group', $group);
        return $this->fetch();
    }

    public function userupdate()
    {
    	$id = input("get.id");

        if ($id !=  session('ad_id') && session('ad_group_id') != 1) {
            $this->assign("message","抱歉，权限不足");
            return $this->fetch("/pub/denied");
        }

    	if (request()->isPost()) {
    	
    		$update["user_name"] = input("post.user_name");
    		$update["group_id"] = input("post.group_id");
    		if (input("post.pass_word")) {
    			$update["pass_word"] = md5(config("afs.pass_prefix").md5(input("post.pass_word")));
    		}

            if (empty($update["user_name"]) || empty($update["group_id"])) {
                $this->error('所有内容不得为空！');
            }else{
                $back = db('ad_users')->where("id = ".$id)->update($update);
                if ($back !== false) {
                    $this->success('修改成功', '/admin/setup/user.html');
                }else{
                    $this->error('修改失败');
                }
            }

        }

        $ad_users = db('ad_users')->where("id = ".$id)->find();
    	$this->assign("ad_users",$ad_users);
        $ad_group = db('ad_group')->select();
        $group = [];
        foreach ($ad_group as $key => $value) {
        	$group[$value["id"]] = $value["name"];
        }
        $this->assign('group', $group);
        return $this->fetch();
    }

    public function userdelete()
    {
        $id = input("get.id");
        $where["id"] = ["in",$id];
        $back = db('ad_users')->where($where)->delete();
        if ($back !== false) {
            $array = ["status" => 1,"message" => "删除成功"];
        }else{
            $array = ["status" => 0,"message" => "删除失败"];
        }
        return json($array);
    }


    public function group()
    {
    	if (input("get.name")) {
            $where["name"] = ["like","%".input("get.name")."%"];
            $this->assign("name",input("get.name"));
        }else{
            $this->assign("name",'');
        }
        $where["id"] = ["gt",0]; 
        $count = db('ad_group')->where($where)->count();
        $page = new \think\Page($count,10);
        $list = db('ad_group')->where($where)->limit($page->firstRow.','.$page->listRows)->select();
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function groupadd()
    {
    	if (request()->isPost()) {
    		
    		$post = input("post.");
    		$add["name"] = $post["name"];
    		unset($post["name"]);
    		$add["auth"] = json_encode($post);
    		if (empty($add["name"])) {
                $this->error('所有内容不得为空！');
            }else{
                $back = db('ad_group')->insert($add);
                if ($back !== false) {
                    $this->success('添加成功', '/admin/setup/group.html');
                }else{
                    $this->error('添加失败');
                }
            }
        }
    	$auth = authAllList();
    	$this->assign('auth', $auth);
        return $this->fetch();
    }

    public function groupupdate()
	{
		$id = input("get.id");
		if (request()->isPost()) {
    		
    		$post = input("post.");
    		$update["name"] = $post["name"];
    		unset($post["name"]);
    		$update["auth"] = json_encode($post);
    		if (empty($update["name"])) {
                $this->error('所有内容不得为空！');
            }else{
                $back = db('ad_group')->where("id = ".$id)->update($update);
                if ($back !== false) {
                    $this->success('修改成功', '/admin/setup/group.html');
                }else{
                    $this->error('修改失败');
                }
            }
        }
        $ad_group = db('ad_group')->where("id = ".$id)->find();
        $ad_group["auth"] = json_decode($ad_group["auth"],true);
    	$this->assign("ad_group",$ad_group);
    	$auth = authAllList();
    	$this->assign('auth', $auth);
        return $this->fetch();
	}

	public function groupdelete()
	{
		$id = input("get.id");
        $where["id"] = ["in",$id];
        $back = db('ad_group')->where($where)->delete();
        if ($back !== false) {
            $array = ["status" => 1,"message" => "删除成功"];
        }else{
            $array = ["status" => 0,"message" => "删除失败"];
        }
        return json($array);
	}

    public function groupcopy()
    {
        $id = input("get.id");

        $ad_group = db('ad_group')->where("id = ".$id)->find();
        unset($ad_group["id"]);
        $back = db('ad_group')->insert($ad_group);
        if ($back !== false) {
            $this->success('复制成功', '/admin/setup/group.html');
        }else{
            $this->error('复制失败');
        }
    }

    public function auth()
    {
        if (input("get.name")) {
            $name = input("get.name");
            $this->assign("name",input("get.name"));
        }else{
            $this->assign("name",'');
        }

        $all_auth_list = getAllAuth();
        $auth_list = [];
        if (isset($name)) {
            foreach ($all_auth_list as $key => $value) {
                if (strpos($value["name"],$name) !== false) {
                    $auth_list[] = $value;
                }
            }
        }else{
            $auth_list = $all_auth_list;
        }

        $count = count($auth_list);
        $page = new \think\Page($count,10);
        $list = array_slice($auth_list,$page->firstRow,$page->listRows);
        $page = $page->show();
        $auth = authTopList();
        $this->assign("auth",$auth);
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function authadd()
    {
    	if (request()->isPost()) {
    		$add["name"] = input("post.name");
    		$add["parent_id"] = input("post.parent_id");
    		// if ($add["parent_id"]) {
    			$add["cname"] = input("post.cname");
    			$add["aname"] = input("post.aname");
    		// }else{
    		// 	$add["cname"] = "";
    		// 	$add["aname"] = "";
    		// }
    		if (empty($add["name"])) {
                $this->error('所有内容不得为空！');
            }else{
                $back = db('ad_auth')->insert($add);
                if ($back !== false) {
                    $this->success('添加成功', '/admin/setup/auth.html');
                }else{
                    $this->error('添加失败');
                }
            }
        }

        $auth = authTopList();
        $this->assign("auth",$auth);
    	return $this->fetch();
    }

    public function authupdate()
    {
    	$id = input("get.id");
    	if (request()->isPost()) {
    		$update["name"] = input("post.name");
    		$update["parent_id"] = input("post.parent_id");
    		if ($update["parent_id"]) {
    			$update["cname"] = input("post.cname");
    			$update["aname"] = input("post.aname");
    		}else{
    			$update["cname"] = "";
    			$update["aname"] = "";
    		}
    		if (empty($update["name"])) {
                $this->error('所有内容不得为空！');
            }else{
                $back = db('ad_auth')->where("id = ".$id)->update($update);
                if ($back !== false) {
                    $this->success('修改成功', '/admin/setup/auth.html');
                }else{
                    $this->error('修改失败');
                }
            }
        }
    	$ad_auth = db('ad_auth')->where("id = ".$id)->find();
    	$this->assign("ad_auth",$ad_auth);
        $auth = authTopList();
        $this->assign("auth",$auth);
    	return $this->fetch();
    }

    public function authdelete()
    {
        $id = input("get.id");
        $where["id"] = ["in",$id];
        $back = db('ad_auth')->where($where)->delete();
        if ($back !== false) {
            $array = ["status" => 1,"message" => "删除成功"];
        }else{
            $array = ["status" => 0,"message" => "删除失败"];
        }
        return json($array);
    }

    public function log()
    {
        
        
        if (input("get.id")) {
            $id = input("get.id");
            $where["ad_id"] = $id;
            $this->assign('id', $id);
        }


        if (input("get.time")) {
            $day = date("Ymd",strtotime(input("get.time")));
            $where["day"] = $day;
            $time = date("Y-m-d",strtotime(input("get.time")));
            $this->assign('time', $time);
            $where1["day"] = $day; 
            $where1["id"] = ["gt",0]; 
            $log = db('ad_log')->field("ad_name,count(id) as num")->where($where1)->group("ad_id")->select();
            $this->assign('log', $log);
            $this->assign('day', $where1["day"]);
        }else{
            $where1["day"] = date("Ymd"); 
            $where1["id"] = ["gt",0]; 
            $log = db('ad_log')->field("ad_name,count(id) as num")->where($where1)->group("ad_id")->select();
            $this->assign('log', $log);
            $this->assign('day', $where1["day"]);
        }

        if (input("get.url")) {
            $url = input("get.url");
            $where["url"] = ["like","%".$url."%"];
            $this->assign('url', $url);
        }else{
            $this->assign('url', '');
        }
        $where["id"] = ["gt",0]; 
        $count = db('ad_log')->where($where)->count();
        $page = new \think\Page($count,10);
        $list = db('ad_log')->where($where)->order("id desc")->limit($page->firstRow.','.$page->listRows)->select();
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch();
    }
}
